package youleche.com.controler.servlet;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import youleche.com.business.BRentVehicleInfo;
import youleche.com.business.BVehicleImageInfo;
import youleche.com.common.ParamConfigParameters;
import youleche.com.controler.servlet.common.UlecheHttpServlet;
import youleche.com.factory.MLLogFactory;
import youleche.com.model.MRentVehicleInfo;
import youleche.com.model.MVehicleImageInfo;
import youleche.com.util.AESEncryptAndDecrypt;
import youleche.com.util.EscapeAndUnescapeUtil;
import youleche.com.util.FileUtil;
import youleche.com.util.ImageFactory;
import youleche.com.util.JsonHelper;

/**
 * Servlet implementation class SRentVehicleInfoServletAuthority
 */
public class SRentVehicleInfoServletAuthority extends UlecheHttpServlet {
	private static final long serialVersionUID = 1L;
       
    public SRentVehicleInfoServletAuthority() {
        super();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		int action = Integer.parseInt(request.getParameter("action"));
		switch(action){
			case 0 :	
				//根据用户名的到租车信息列表
				this.getCountByName(request, response);
				break;
			case 1:
				//根据用户名得到租车信息总条数
				this.getListByName(request, response);
				break;
			case 2:
				//根据租车ID删除租车信息
				this.deleteInfoById(request, response);
				break;
		}
	}
		
	/**
	 * 根据ID删除租车信息
	 * @param request
	 * @param response
	 */
	private void deleteInfoById(HttpServletRequest request, HttpServletResponse response){
		String userName = this.getUserNameByCookie(request, response);
		if("".equals(userName)){
			super.response(response, "0");
			return;
		}
		
		String id = request.getParameter("id");
		BRentVehicleInfo handler = new BRentVehicleInfo();
		try {
			if(handler.deleteRentInfoById(id) > 0){
				BVehicleImageInfo imgHandler = new BVehicleImageInfo();
				List<MVehicleImageInfo> imageInfos = imgHandler.getImageInfoByVehicleId(id);
				for (MVehicleImageInfo imgInfo : imageInfos) {
					FileUtil.deleteFile(imgInfo.getImagePath(), request.getSession().getServletContext().getRealPath("/") + "vehicleImage/");
				}
				
				super.response(response, "1");
				return;
			}
		} catch (Exception e) {
			MLLogFactory.exceptionLog("删除租车信息失败", "根据ID删除租车信息", e.getMessage(), request.getRemoteAddr());
			e.printStackTrace();
			super.response(response, "0");
			return;
		}
		
		super.response(response, "0");
	}
	
	/**
	 * 根据用户名得到租车信息总条数
	 * @param request 请求
	 * @param response 响应
	 */
	private void getCountByName(HttpServletRequest request, HttpServletResponse response){
		String userName = this.getUserNameByCookie(request, response);
		if("".equals(userName)){
			super.response(response, "0");
			return;
		}
		
		BRentVehicleInfo handler = new BRentVehicleInfo();
		try {
			int count = handler.getRentInfoCountByUserName(userName);
			super.response(response, ""+count+"");
		} catch (Exception e) {
			super.response(response, "0");
		}
	}
	
	/**
	 * 根据用户名的到租车信息列表
	 * @param request 请求
	 * @param response 响应
	 */
	private void getListByName(HttpServletRequest request, HttpServletResponse response){
		String userName = this.getUserNameByCookie(request, response);
		if("".equals(userName)){
			super.response(response, "");
			return;
		}
		
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		Integer startIndex = (Integer.parseInt(request.getParameter("pageIndex")) - 1) * pageSize ;
		
		BRentVehicleInfo handler = new BRentVehicleInfo();
		try {
			List<MRentVehicleInfo> list = handler.getRentVehicleInfoForListByUserName(startIndex.toString(), ((Integer)pageSize).toString(), userName);
			List<MRentVehicleInfo> switchList = null;
			if(list != null && list.size() > 0){
				switchList = new LinkedList<MRentVehicleInfo>();
				ParamConfigParameters paramConfig = new ParamConfigParameters();
				String key = paramConfig.getParameter("LinkNum_KEY");
				for (MRentVehicleInfo rentInfo : list) {
					rentInfo.setLinkPhone(AESEncryptAndDecrypt.decrypt(rentInfo.getLinkPhone(), key));
					switchList.add(rentInfo);
				}
			}
			
			JsonHelper.printListToJsonString(response, switchList);
		} catch (Exception e) {
			super.response(response, "");
		}
	}
	
	/**
	 * 通过cookie得到用户名
	 * @param request 请求
	 * @param response 响应
	 * @return 用户名
	 */
	private String getUserNameByCookie(HttpServletRequest request, HttpServletResponse response){
		Cookie[] cookies = request.getCookies();
		if(cookies == null || cookies.length == 0 ||cookies.length == 1){
			return "";
		}
		String userName = "";
		for(int i=0 ;i<cookies.length;i++){
			if(cookies[i].getName().equals("youleche.u")){
				String cookieVlaue = cookies[i].getValue();
				String cookieJson = EscapeAndUnescapeUtil.unescape(cookieVlaue);
				userName = cookieJson;
				break;
			}
		}
		
		return userName;
	}

}
